#!/bin/sh
#
# Run a simple backup
#   prune file records, then restore only README files.
#
TestName="fileregexp-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="${tmp}/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp ${conf}/susan-dir.conf ${tmp}/1
sed -f ${outf} ${tmp}/1 >${conf}/susan-dir.conf

$bperl -e "add_attribute('${conf}/susan-dir.conf', 'File Retention', '2', 'Client')"

change_jobname BackupClient1FileList $JobName

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list

mkdir -p ${BackupDirectory}/test/
echo "test" > ${BackupDirectory}/test/t1.txt
echo "test" > ${BackupDirectory}/test/t2.txt
echo "test" > ${BackupDirectory}/test/t3.txt

start_test

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
label volume=TestVolume001 storage=File pool=Full
run job=$JobName yes
wait
messages
@sleep 5
prune files yes
quit
END_OF_DATA

run_susan

# to make incremental backup
echo "test" > ${BackupDirectory}/inc1.dat

cat <<END_OF_DATA >${tmp}/bconcmds
run job=$JobName yes
wait
messages
@#
@# now do a restore
@#
@$out ${tmp}/log2.out
restore where=${tmp}/susan-restores
5
no
.*\.txt
yes
wait
messages
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=File
stop_susan

#
# The above restore should have restored some directories, but only
# files that have *.txt in their name.  There should in fact, be
# no other files restored COUNT_OTHER==0 and more then 0 files restored.
#
COUNT_OTHER=`find ${tmp}/susan-restores -type f | grep -v .txt$ | wc -l`
COUNT_README=`find ${tmp}/susan-restores -type f | grep .txt$ | wc -l`

if [ $COUNT_OTHER -gt 0 -o $COUNT_README -lt 1 ]; then
    print_debug "Found non matching files"
    print_debug "Count_other=$COUNT_OTHER count_readme=$COUNT_README"
    print_debug `find ${tmp}/susan-restores -type f | grep -v README`
    rstat=2
fi

#check_two_logs         # don't work because of "warning file count mismatch"
grep "Backup OK" ${tmp}/log1.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "Found error in backup"
    bstat=2
fi

end_test
